<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
  <el-form-item label="学生姓名">
    <el-col :span="5"> 
    <el-input v-model="form.name"></el-input>
    </el-col>
  </el-form-item>
  <el-form-item label="学号">
    <el-col :span="5">
    <el-input v-model="form.number"></el-input>
    </el-col>
  </el-form-item>
  <el-form-item label="所在班级">
    <el-select v-model="form.class.id" placeholder="请选择所在班级">
      <el-option :key="this.form.class.id" :label="this.form.class.cname" :value="this.form.class.id">
        </el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="onSubmit">立即添加</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>
  </div>
</template>
<script>
import axios from 'axios';
import commons from "../../../assets/commons";
export default {
    data() {
      return {
        form: {
            name: '',
            number:'',
            class: {
              id:0,
              cname:''
            },
        },
        c_id:1,
      }
    },
    methods: {
      onSubmit() {
        let params = new URLSearchParams();
      //封装数据
        params.append("name", this.form.name);
        params.append("number", this.form.number);
        params.append("c_id", this.form.class.id);
        axios.post("http://localhost:8080/student_account_management/addStuUser", params).then(res => { 
          if(res.data.code == 200){
            this.$message({
              type: "success",
              message:res.data.message
            });    
          }
          if(res.data.code == 201){
          this.$message({
            type: "error",
            message:res.data.message
          });    
          }
        });
      },
      searchClassByC_id(){
        axios.get(
          commons.baseUrl +
            "student_account_management/searchClassByC_id?c_id=" +
            this.c_id
        ).then(res => { 
          this.form.class.id=res.data.data.id;
          this.form.class.cname=res.data.data.cname;
        });
      }
     
    },
     mounted(){
        this.searchClassByC_id();
      },computed: {}
  }
</script>
<style scoped></style>
